package com.abu.authms.controller;

import com.abu.authms.client.UmsClient;
import com.abu.authms.properties.JwtProperties;
import com.abu.authms.service.AuthService;
import com.abu.utils.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/auth")
@EnableConfigurationProperties(JwtProperties.class)
public class LoginAuthController {

    @Autowired
    private AuthService authService;

    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("/login")
    public String login(
            @RequestParam("nickname") String nickname,
            @RequestParam("password") String password,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
//        根据用户名密码获取token
        String token = this.authService.getToken(nickname, password);
//        将token放入cookie中
        CookieUtils.setCookie(request, response, jwtProperties.getCookieName(), token, jwtProperties.getExpire() * 11);
        return "登陆成功";
    }



}
